﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Remember.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : NavigationWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            Init();
            LoginStart();
        }

        public static NavigationWindow CurrentWindow= null;
        public static string HomePageUrl = "Pages/BookListPage.xaml";
        public static string LoginPageUrl = "Pages/LoginPage.xaml";

        public DoubleAnimation animFadeIn = new DoubleAnimation();
        public DoubleAnimation animFadeOut = new DoubleAnimation();

        void Init()
        {

            animFadeIn = new DoubleAnimation();
            animFadeIn.From = 0;
            animFadeIn.To = 1;
            animFadeIn.Duration = new Duration(TimeSpan.FromMilliseconds(1500));
            //this.BeginAnimation(OpacityProperty, animFadeIn);

            animFadeOut = new DoubleAnimation();
            animFadeOut.From = 1;
            animFadeOut.To = 0;
            animFadeOut.Duration = new Duration(TimeSpan.FromMilliseconds(500));

            CurrentWindow = this;
        }

        void LoginStart()
        {
            this.NavigationService.Navigate(new System.Uri(LoginPageUrl, System.UriKind.Relative));
        }

        void BookPage()
        {
            this.NavigationService.Navigate(new System.Uri(HomePageUrl, System.UriKind.Relative));
        }

        private void NavigationWindow_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                MaxCancel();
            }

            if (e.Key == Key.F11)
            {
                Max();
                //this.Topmost = true;
                //Cursor = Cursors.None;
            }

        }

        public void MaxCancel()
        {
            this.WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
            this.ResizeMode = System.Windows.ResizeMode.CanResize;
            //Cursor = Cursors.Arrow;

        }

        public void Max()
        {
            this.WindowState = System.Windows.WindowState.Normal;
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.WindowState = System.Windows.WindowState.Maximized;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;

        }

        private void NavigationWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.Source != null && (this.Source.OriginalString == HomePageUrl || this.Source.OriginalString == LoginPageUrl ) )
            {
                var rc = MessageBox.Show("Are you going to exit?", "", MessageBoxButton.YesNo);
                if (rc != MessageBoxResult.Yes)
                {
                    e.Cancel = true;

                     
                }
            }
            else
            {
                e.Cancel = true;
                LoginStart ();
            }

        }

        private void NavigationWindow_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        private void NavigationWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void NavigationWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            return;
                var page = this.NavigationService.Content;
                if (page is Page)
                {
                    var currentPage = page as Page;

                    var width = this.Width;
                    var height = this.Height;
                    ScaleTransform scaleTransform = new ScaleTransform();

                    if (width < 800 || height < 600)
                    {
                        scaleTransform.ScaleX = width / 1024;
                        scaleTransform.ScaleY = height / 800 ;
                        currentPage.RenderTransform = scaleTransform;

                    }

                    if (width >= 800 || height >= 600)
                    {
                        scaleTransform.ScaleX = 1;
                        scaleTransform.ScaleY = 1;
                        currentPage.RenderTransform = scaleTransform;
                    }
                }
            return;

        }
    }
}
